package com.example.planetandzombiedemo.entity;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.method.Touch;
import android.view.MotionEvent;

import com.example.planetandzombiedemo.global.Config;
import com.example.planetandzombiedemo.model.BaseModel;
import com.example.planetandzombiedemo.model.Touchable;

public class Sun extends BaseModel implements Touchable {
    //阳光范围内
    private Rect touchArea;
    //阳光产生的时间
    private long birthTime;
    //标志阳光的状态
    private SunState state;
    //移动距离
    private float directionDistanceX;
    private float directionDistanceY;
    //移动速度
    private float speedX;
    private float speedY;

    public Sun(int locationX, int locationY) {
        this.locationX = locationX;
        this.locationY = locationY;
        isAlife = true;
        touchArea = new Rect(locationX, locationY,
                locationX + Config.sun.getWidth() / 2, locationY + Config.sun.getHeight());
        birthTime = System.currentTimeMillis();
        //设置阳光显示
        state = SunState.SHOW;
    }

    @Override
    public void drawSelf(Canvas canvas, Paint paint) {
        if (isAlife) {
            if (state == SunState.SHOW)//静止显示状态
            {
                //5000毫秒后阳光消失
                if ((System.currentTimeMillis() - birthTime) > 5000) {
                    isAlife = false;
                }
            } else {//移动显示状态
                //移动
                locationX -= speedX;
                locationY -= speedY;
                //屏幕上方阳光消失，阳光值增加
                if (locationY <= 0) {
                    isAlife = false;
                    Config.sunLight += 25;
                }

            }
            canvas.drawBitmap(Config.sun,locationX,locationY,paint);
        }
    }

    @Override
    public int getModelWidth() {
        return Config.sun.getWidth();
    }

    @Override
    public boolean onTouch(MotionEvent event) {
        int x=(int)event.getRawX();
        int y=(int)event.getRawY();
        if(touchArea.contains(x,y))
        {
            //拾取阳光
            state=SunState.MOVE;
            directionDistanceX=locationX-Config.seedBankLocationX;
            directionDistanceY=locationY;
            speedX=directionDistanceX/20.0f;
            speedY=directionDistanceY/20.0f;
            return true;
        }
        return false;
    }

    //阳光状态
    public enum SunState {
        SHOW, MOVE
    }
}
